﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using ProjectRoughWork.Engine.SoundManager;

namespace ProjectRoughWork
{
    class MainMenuScreen : GameScreen
    {
        /// <summary>
        /// Default Constructor. Initializes the list of menu components
        /// </summary>

        public MainMenuScreen()
        {
            componentList = new List<MenuComponent>();
            Init();
        }

        /// <summary>
        /// Initializes the menu components as well as the selected component
        /// </summary>

        public void Init()
        {
            GraphicsDevice device = Game1.GetGraphicsDevice();

            Vector2 initPos = new Vector2(device.Viewport.Width/2, device.Viewport.Height/2);
            Rectangle bb = new Rectangle((int)initPos.X, (int)initPos.Y, 200, 50);
            Texture2D background = Game1.GetContentManager().Load<Texture2D>("MenuBg");

            MenuComponent start = new MenuComponent(bb, "Start Game", background);
            bb.Y += bb.Height + 5;
            MenuComponent exit = new MenuComponent(bb, "Exit", background);

            // Get an instance of the SoundManager and begin playing background music
            SoundManager.GetInstance().playBackgroundMusic(SoundManager.BGM_MENU_MAIN);

            componentList.Add(start);
            componentList.Add(exit);

            selectedComponent = 0;
            componentList[selectedComponent].TextColor = Color.White;
        }
        
        /// <summary>
        /// Handles keyboard input from the user. 
        /// </summary>
        /// <param name="inputKey"></param>

        public override void HandleInput(KeyboardState inputKey)
        {

            if ((inputKey.IsKeyDown(Keys.Down)) && (lastKeyInput.IsKeyUp(Keys.Down)) )
            {
                //Move the selected component up 1 component, wrapping around if necessary

                componentList[selectedComponent].TextColor = Color.Red;
                selectedComponent++;
                if (selectedComponent == componentList.Count)
                {
                    selectedComponent = 0;
                }
            }
            else if ((inputKey.IsKeyDown(Keys.Up)) && (lastKeyInput.IsKeyUp(Keys.Up)))
            {
                //Move the selected component down 1 component, wrapping around if necessary

                componentList[selectedComponent].TextColor = Color.Red;
                selectedComponent--;
                if (selectedComponent == -1)
                {
                    selectedComponent = componentList.Count - 1;
                }
            }
            else if ((inputKey.IsKeyDown(Keys.Enter)) && (lastKeyInput.IsKeyUp(Keys.Enter)))
            {
                //Handles the execution of the currently selected component

                HandleSelection();
            }

            //Updates color of selected component and stores this keyboard input for reference in the next update frame

            componentList[selectedComponent].TextColor = Color.White;
            lastKeyInput = inputKey;
            base.HandleInput(inputKey);
        }

        /// <summary>
        /// Handles Mouse input
        /// </summary>
        /// <param name="inputClick">The state of the mouse</param>

        public override void HandleInput(MouseState inputClick)
        {
            for (int index = 0; index < componentList.Count; index++ )
            {
                if (componentList[index].CheckIfClicked(inputClick.X, inputClick.Y))
                {
                    componentList[selectedComponent].TextColor = Color.Red;
                    componentList[index].TextColor = Color.White;
                    selectedComponent = index;
                }
            }

            base.HandleInput(inputClick);
        }

        public override void HandleInput(GamePadState inputButton)
        {
            if ((inputButton.IsButtonDown(Buttons.DPadDown)) && (lastButtonInput.IsButtonUp(Buttons.DPadDown)))
            {
                //Move the selected component up 1 component, wrapping around if necessary

                componentList[selectedComponent].TextColor = Color.Red;
                selectedComponent++;
                if (selectedComponent == componentList.Count)
                {
                    selectedComponent = 0;
                }
            }
            else if ((inputButton.IsButtonDown(Buttons.DPadUp)) && (lastButtonInput.IsButtonUp(Buttons.DPadUp)))
            {
                //Move the selected component down 1 component, wrapping around if necessary

                componentList[selectedComponent].TextColor = Color.Red;
                selectedComponent--;
                if (selectedComponent == -1)
                {
                    selectedComponent = componentList.Count - 1;
                }
            }
            else if ((inputButton.IsButtonDown(Buttons.A)) && (lastButtonInput.IsButtonUp(Buttons.A)))
            {
                //Handles the execution of the currently selected component

                HandleSelection();
            }

            //Updates color of selected component and stores this keyboard input for reference in the next update frame

            componentList[selectedComponent].TextColor = Color.White;
            lastButtonInput = inputButton;
        }

        /// <summary>
        /// Executes the necessary actions for selecting a particular menu component
        /// </summary>

        public void HandleSelection()
        {
            switch (selectedComponent)
            {
                case 0:
                    //Start Game
                    SquadSelectionScreen ssScreen = new SquadSelectionScreen();
                    EventChangeScreen changeScreenEvt = new EventChangeScreen(ssScreen);
                    EventManager.GetInstance().VTriggerEvent(changeScreenEvt);
                    //EventChangeGameState stateEvt = new EventChangeGameState(ProjectGameLogic.GameState.Playing);
                    //EventManager.GetInstance().VTriggerEvent(stateEvt);
                    break;
                case 1:
                    //Exit
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// Draws the main menu.
        /// </summary>

        public override void Draw()
        {
            GraphicsDevice device = Game1.GetGraphicsDevice();
            device.Clear(Color.Black);

            //Retrieve sprite batch and sprite font

            SpriteBatch spriteBatch = Game1.GetSpriteBatch();
            SpriteFont headerFont = Game1.GetHeaderFont();

            //Compute the position of the header font

            Vector2 textOrigin = headerFont.MeasureString("Tactical Nerf Skirmisher");
            textOrigin.X = device.Viewport.Width/2 - textOrigin.X/2;
            textOrigin.Y = device.Viewport.Height/10 + textOrigin.Y/2;

            spriteBatch.Begin();

            spriteBatch.DrawString(headerFont, "Tactical Nerf Skirmisher", textOrigin, Color.Red);

            spriteBatch.End();

            foreach(MenuComponent component in componentList)
            {
                component.Draw();
            }
        }

        //Data Members

        List<MenuComponent> componentList;          //list of components that make up the menu
        int selectedComponent;                      //index of the currently selected component

        //KeyboardState lastKeyInput;                 //The state of the keyboard from the previous frame
    }
}
